package com.zhang.hadoop.netty.nio.buffer;

import java.nio.ByteBuffer;

/**
 * @author: zhang yufei
 * @create: 2022-01-09 17:45
 * @description: 只读buffer
 **/
public class ReadOnlyBuffer {
    public static void main(String argsp[]) {
        ByteBuffer buffer = ByteBuffer.allocate(64);

        for (int i = 0; i < 64; i++) {
            buffer.put((byte) i);
        }

        //读取
        buffer.flip();

        //得到一个制度的buffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
        System.out.println(readOnlyBuffer.getClass());

        //读取
        while (readOnlyBuffer.hasRemaining()) {
            System.out.println(readOnlyBuffer.get());
        }

        //会抛出异常ReadOnlyBufferException
        readOnlyBuffer.put((byte) 100);
    }
}
